#include<iostream>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
using std::cout;

using std::cin;
using std::endl;


void fun(int signum)
{
    cout<<"catch signal :"<<signum<<endl;
    exit(1);
}

void PrintPending(const sigset_t &pending)
{
    std::cout << "curr pending list [" << getpid() << "]: " ;
    for(int signo = 63; signo > 0; signo--)
    {
        if(sigismember(&pending, signo))
        {
            std::cout << 1;
        }
        else
        {
            std::cout << 0;
        }
    }
    std::cout << std::endl;
}
void PrintBlock(const sigset_t &block)
{
    std::cout << "curr block list [" << getpid() << "]: " ;
    for(int signo = 63; signo > 0; signo--)
    {
        if(sigismember(&block, signo))
        {
            std::cout << 1;
        }
        else
        {
            std::cout << 0;
        }
    }
    std::cout << std::endl;
}
int main()
{
    // signal(SIGINT,fun);
    struct sigaction act;
    act.sa_handler = fun;
    ::sigaction(SIGINT,&act,nullptr);

    sigset_t sigset,oldsigset;
    sigemptyset(&sigset);
    sigaddset(&sigset,2);
    sigaddset(&sigset,40);

    sigprocmask(SIG_BLOCK,&sigset,&oldsigset);

    while(true)
    {    
        // std::cout<<"running..."<<std::endl;
        sigset_t pending,block;
        sigpending(&pending);
        sigprocmask(0, NULL, &block);
        
        PrintBlock(block);
        PrintPending(pending);
        sleep(1);
    }



    return 0;
}